package com.bigdatastudio.other.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdatastudio.auth.utils.TokenUtil;
import com.bigdatastudio.common.Utils.PageUtil;
import com.bigdatastudio.common.Utils.R;
import com.bigdatastudio.common.Utils.isDeleteUtil;
import com.bigdatastudio.common.entity.HonorWall;
import com.bigdatastudio.other.server.IHonorWallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiao_jie
 * @since 2022-09-12
 */
@RestController
@RequestMapping("/other/honor-wall")
public class HonorWallController {

    @Autowired
    private IHonorWallService honorWallService;


    @PostMapping("add")
    public R add(@RequestBody HonorWall honorWall) {
        honorWall.setCreateName(TokenUtil.getJwtUser().getCreateName());
        honorWall.setUpdateName(TokenUtil.getJwtUser().getUpdateName());
        honorWall.setHwPushTime(LocalDateTime.now());
        honorWall.setUpdateTime(LocalDateTime.now());
        honorWallService.save(honorWall);
        return R.ok("添加成功");
    }

    @PostMapping("/list")
    public R page(@RequestBody PageUtil pageUtil) {
        String option = "分页查询";
        Page<HonorWall> page1 = new Page<>(pageUtil.getCurrent(), pageUtil.getSize());
        LambdaQueryWrapper<HonorWall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(HonorWall::getIsDelete, 1);
        queryWrapper.orderByAsc(HonorWall::getHwPushTime);
        Page<HonorWall> page2 = honorWallService.page(page1, queryWrapper);
        return isDeleteUtil.isdelete(0, page2, option);
    }

    @GetMapping("/delete")
    public R delete(Integer id) {
        String option = "删除";
        UpdateWrapper<HonorWall> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("is_delete", 1);
        honorWallService.update(updateWrapper);
        return isDeleteUtil.isdelete(0, option);
    }

    @PostMapping("/update")
    public R update(@RequestBody HonorWall honorWall) {
        String option = "修改";
        LambdaQueryWrapper<HonorWall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HonorWall::getId, honorWall.getId());
        HonorWall one = honorWallService.getOne(queryWrapper);
        if (one.getIsDelete() != 0) {
            return isDeleteUtil.isdelete(1, option);
        } else {
            honorWallService.updateById(honorWall);
            return isDeleteUtil.isdelete(0, option);
        }
    }

    @GetMapping("/getOne")
    public R getOne(Integer id) {
        String option = "获取单个";
        LambdaQueryWrapper<HonorWall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HonorWall::getId, id);
        HonorWall one = honorWallService.getOne(queryWrapper);
        return isDeleteUtil.isdelete(one.getIsDelete(), one, option);
    }


    @GetMapping("/batchdelete")
    public R batchdelete(Integer[] id) {
        String option = "批量删除";
        UpdateWrapper<HonorWall> updateWrapper = new UpdateWrapper<>();
        for (int i = 0; i < id.length; i++) {
            updateWrapper.eq("id", id[i]);
            updateWrapper.set("is_delete", 1);
            honorWallService.update(updateWrapper);
            updateWrapper.clear();
        }
        return isDeleteUtil.isdelete(0, option);
    }
}
